package com.example.elderlycare.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.elderlycare.dao.entity.Elder;
import com.example.elderlycare.dao.mapper.ElderMapper;
import com.example.elderlycare.service.ElderService;
import com.example.elderlycare.service.entity.ElderItem;
import com.example.elderlycare.service.entity.ElderListResponseBody;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ElderServiceImpl extends ServiceImpl<ElderMapper, Elder> implements ElderService {
    @Resource
    ElderMapper elderMapper;

    @Override
    public ElderListResponseBody queryElders(Map<String, Object> params) {
        ElderListResponseBody elderListResponseBody = new ElderListResponseBody();
        int total = elderMapper.getElderCount(params);
        elderListResponseBody.setTotal(total);
        List<Elder> elders = elderMapper.queryElders(params);
        List<ElderItem> items = new ArrayList<>();
        for (Elder elder : elders) {
            ElderItem item = ElderItem.builder()
                    .id(elder.getId())
                    .name(elder.getName())
                    .sex(elder.getSex())
                    .certificateName(elder.getCertificateName())
                    .birthday(elder.getBirthday())
                    .age(new Date().getYear() - elder.getBirthday().getYear())
                    .phone(elder.getPhone())
                    .number(elder.getNumber())
                    .build();

            items.add(item);
        }
        elderListResponseBody.setElderItemList(items);
        return elderListResponseBody;
    }
}
